﻿Imports System.Runtime.CompilerServices
Imports System.Web.Script.Serialization
Imports System.Text

Public Module JsonUtils

    <Extension()>
    Public Function JsonSerializer(ByVal parameters As IDictionary(Of String, String)) As String
        Dim sortedParams As IDictionary(Of String, String) = New SortedDictionary(Of String, String)(parameters)
        Dim dem As IEnumerator(Of KeyValuePair(Of String, String)) = sortedParams.GetEnumerator()
        Dim query As New StringBuilder("{")
        While dem.MoveNext()
            Dim key As String = dem.Current.Key
            Dim value As String = dem.Current.Value
            If Not String.IsNullOrEmpty(key) AndAlso Not String.IsNullOrEmpty(value) Then
                query.Append(String.Format("""{0}"": ""{1}"",", key, value))
            End If
        End While
        If sortedParams.Count > 0 Then
            query = query.Remove(query.Length - 1, 1)
        End If
        Return query.Append("}").ToString
    End Function

End Module
